<?php

namespace App\Http\Requests\Api;

class CaptchasRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'action' => [
                'required',
                function ($attribute, $value, $fail) {
                    if (!in_array($value,['register','reset'])) {
                        return $fail('action数据错误');
                    }
                },
            ],
        ];

        if ($this->action == 'register') {
            $rules['email'] = 'required|string|unique:users|max:255';
        } else {
            $rules['email'] = 'required|string|exists:users,email|max:255';
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'action.accepted' => '接受信息错误'
        ];
    }

}
